package com.kbs.im.server.handler;

import com.kbs.im.common.protocol.packet.request.ListAllDeviceRequestPacket;
import com.kbs.im.common.protocol.packet.response.ListAllDeviceResponsePacket;
import com.kbs.im.server.utils.SessionManager;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhangzhang
 */
public class ListAllDeviceRequestHandler extends SimpleChannelInboundHandler<ListAllDeviceRequestPacket> {

    public static final ListAllDeviceRequestHandler INSTANCE = new ListAllDeviceRequestHandler();

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ListAllDeviceRequestPacket msg) throws Exception {
        List<String> deviceCPUList = new ArrayList<>();
        for (Map.Entry<String, Channel> entry : SessionManager.DEVICE_CHANNEL_MAP.entrySet()) {
            deviceCPUList.add(entry.getKey());
        }
        ListAllDeviceResponsePacket responsePacket = new ListAllDeviceResponsePacket();
        responsePacket.setDeviceCPUList(deviceCPUList);
        System.out.println("xxxxxxxxxxxxxxx"+responsePacket.toString());
        ctx.channel().writeAndFlush(responsePacket);
    }
}
